package com.xu.sso.web.controller;

import com.xu.sso.core.exception.SSOException;
import com.xu.sso.core.model.BaseResponse;
import com.xu.sso.db.model.base.ApiAuth;
import com.xu.sso.db.model.base.App;
import com.xu.sso.db.model.base.Department;
import com.xu.sso.db.model.base.User;
import com.xu.sso.web.model.request.UpdateDepRequest;
import com.xu.sso.web.model.request.UpdateUserRequest;
import com.xu.sso.web.model.response.ApiKeyResponse;
import com.xu.sso.web.service.AppService;
import com.xu.sso.web.service.ManageService;
import com.xu.sso.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/manage")
public class ManageController {

    @Autowired
    private UserService userService;
    @Autowired
    private ManageService manageService;
    @Autowired
    private AppService appService;

    @RequestMapping("/toPerson")
    public String toPerson(Model model) {
        List<User> users = userService.findAllUser();
        model.addAttribute("users", users);
        return "manage/person";
    }

    @RequestMapping("/toAddUser")
    public String toAddUser(Model model) {
        List<Department> list = manageService.findAllDepartment();
        model.addAttribute("deps", list);
        return "manage/register";
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public BaseResponse addUser(User user) throws SSOException {
        return userService.addUser(user);
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    public BaseResponse deleteUser(String username) throws SSOException {
        return userService.deleteUser(username);
    }

    @RequestMapping("/toUpdateUser")
    public String toUpdateUser(Model model, String username) {
        User user = userService.findUser(username);
        List<Department> list = manageService.findAllDepartment();
        model.addAttribute("deps", list);
        model.addAttribute("user", user);
        return "manage/updateUser";
    }

    @RequestMapping("/updateUser")
    @ResponseBody
    public BaseResponse updateUser(UpdateUserRequest request) throws SSOException {
        return userService.updateUser(request);
    }

    @RequestMapping("/resetUser")
    @ResponseBody
    public BaseResponse resetUser(String username) throws SSOException {
        return userService.resetUser(username);
    }

    @RequestMapping("/toDepartment")
    public String toDepartment(Model model) {
        List<Department> list = manageService.findAllDepartment();
        model.addAttribute("deps", list);
        return "manage/department";
    }

    @RequestMapping("/toAddDep")
    public String toAddDep(Model model) {
        List<Department> list = manageService.findAllDepartment();
        model.addAttribute("deps", list);
        return "manage/addDep";
    }

    @RequestMapping("/addDep")
    @ResponseBody
    public BaseResponse addDep(Department department) throws SSOException {
        return manageService.createDepartment(department);
    }

    @RequestMapping("/deleteDep")
    @ResponseBody
    public BaseResponse deleteDep(Integer id) throws SSOException {
        return manageService.deleteDep(id);
    }

    @RequestMapping("/toUpdateDep")
    public String toUpdateDep(Model model, Integer id) {
        Department department = manageService.findDepartmentById(id);
        List<Department> list = manageService.findAllDepartment();
        model.addAttribute("deps", list);
        model.addAttribute("dep", department);
        return "manage/updateDep";
    }

    @RequestMapping("/updateDep")
    @ResponseBody
    public BaseResponse updateDep(UpdateDepRequest request) throws SSOException {
        return manageService.updateDepartment(request);
    }

    @RequestMapping("/toOpenApi")
    public String toOpenApi(Model model) {
        List<ApiAuth> list = manageService.findAllApiAuths();
        model.addAttribute("apis", list);
        return "manage/openApi";
    }

    @RequestMapping("/toAddOpenApi")
    public String toAddOpenApi(Model model) {
        List<App> apps = appService.findAllStartApp();
        model.addAttribute("apps", apps);
        return "manage/addOpenApi";
    }

    @RequestMapping("/toUpdateApi")
    public String toUpdateApi(Model model, Integer id) {
        ApiAuth apiAuth = manageService.findApiAuthById(id);
        model.addAttribute("apiAuth", apiAuth);
        return "manage/updateOpenApi";
    }

    @RequestMapping("/updateApi")
    @ResponseBody
    public BaseResponse updateApi(ApiAuth apiAuth) throws SSOException {
        return manageService.updateApiAuth(apiAuth);
    }

    @RequestMapping("/addOpenApi")
    @ResponseBody
    public BaseResponse addOpenApi(ApiAuth apiAuth) throws SSOException {
        return manageService.createApiAuth(apiAuth);
    }

    @RequestMapping("/createKey")
    @ResponseBody
    public ApiKeyResponse createKey() throws SSOException {
        return manageService.createApiKey();
    }

    @RequestMapping("/changeApiStatus")
    @ResponseBody
    public BaseResponse changeApiStatus(Integer id, String status) throws SSOException {
        return manageService.changeApiStatus(id, status);
    }

    @RequestMapping("/deleteApi")
    @ResponseBody
    public BaseResponse deleteApi(Integer id) throws SSOException {
        return manageService.deleteApiAuth(id);
    }

    @RequestMapping("/toRefresh")
    public String toRefresh() {
        return "manage/refresh";
    }

    @RequestMapping("/refresh")
    @ResponseBody
    public BaseResponse refresh() throws SSOException {
        return manageService.refreshRedis();
    }

}
